<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CommentController extends Controller
{

//======================评价列表开始=========================== 
    //show 显示评价列表
    public function show(Request $request)
    {
    	$title = "评价管理";

      //会话控制
      $staff = $request->session()->get('staff');

    	$data = DB::table('bro_comment') -> where(function($query) use ($request){
            $query -> where('uname','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',10));;
    	return view('admin.comment.show',['title' => $title,'data' => $data,'request' => $request -> all(),'staff' => $staff]);
    }
    //delete 删除评价
    public function delete($id)
    {
    	// $data = DB::table('bro_comment') ->  where('id',$id) -> first();
    	// dd($data);
	 	$res = DB::table('bro_comment')-> where('id',$id) -> delete();
	   //判断删除是否成功
       if($res)
        {
            return redirect('/admin/comment/show') -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }
    }
    //add 添加评论回复
    public function add(Request $request)
    {
    	$request -> flashOnly('id');
    	$title = "回复评论";
      //会话控制
      $staff = $request->session()->get('staff');

  		return view('admin.reply.add',['title' => $title,'request' => $request,'staff'=>$staff]);
    }

    //insert 处理添加评论回复
    public function insert(Request $request)
    {
    	$data = $request -> except('_token');
    	// dd($data);
    	// $data = get_object_vars($request->session()->all()['user'])['id'];
    	//将数据发送到数据库
       $res = DB::table('bro_reply') -> insert($data);

       //执行发送判断
       if($res)
       {
         return redirect('/admin/comment/show') -> with('success','评论添加成功');
       }else{
         return back() -> with('error','评论添加失败');
       }
    	
    }

//====================评价列表结束========================================  


//======================回复列表开始======================================
    //reply 显示回复列表
    public function reply(Request $request)
    {
      $title = "评价回复管理";

      //会话控制
      $staff = $request->session()->get('staff');

      $data = DB::table('bro_reply') -> where(function($query) use ($request){
            $query -> where('user_id','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',10));;
      return view('admin.reply.reply',['title' => $title,'data' => $data,'request' => $request -> all(),'staff' => $staff]);
    }


     //del删除回复
    public function del($id)
    {
      $res = DB::table('bro_reply')-> where('id',$id) -> delete();
       //判断删除是否成功
         if($res)
          {
              return redirect('/admin/reply/reply') -> with('success','删除成功');
          }else{
              return back() -> with('error','删除失败');
          }
    }
//======================回复列表结束======================================
}
